package com.app.micaihu.bean.news;

import android.graphics.Bitmap;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.app.micaihu.R;
import com.app.micaihu.utils.HttpUtils;
import com.app.micaihu.videoplayer.JCUserActionStandard;
import com.app.micaihu.videoplayer.JCVideoPlayer;
import com.app.micaihu.videoplayer.JCVideoPlayerStandard;

/**
 * 视频栏目(视频列表)
 * Created by linkai on 2016/7/18.
 */
public class ViewVideo extends ViewNewsParent implements JCUserActionStandard {

    public TextView period;//期数
    public JCVideoPlayerStandard jcVideoPlayer;//播放器
    public ImageView attrImg;//属性图片
    public TextView playCount;//播放次数

    public ViewVideo(View convertView) {
        initView(convertView);
    }

    public void initView(View convertView) {
        super.initView(convertView);
        period = (TextView) convertView.findViewById(R.id.item_period);
        jcVideoPlayer = (JCVideoPlayerStandard) convertView.findViewById(R.id.item_videoplayer);
//        jcVideoPlayer.setStartPlayHanlder(this);
        jcVideoPlayer.setShowTitle(false);
        jcVideoPlayer.setJcUserAction(this);
        attrImg = (ImageView) convertView.findViewById(R.id.item_attricon);
        //用时间的控件显示播放次数
        publishTime = null;
        playCount = (TextView) convertView.findViewById(R.id.item_public_time);
        convertView.setTag(this);
    }

    /**
     * 加载数据
     *
     * @param newsEntity
     */
    public void loadData(NewsEntity newsEntity) {
        super.loadData(newsEntity);
        this.commentCount.setText(newsEntity.getCommentNum() + "评论");
        playCount.setText(newsEntity.getClickNum());
        //加载视频
        jcVideoPlayer.setShowTitle(false);
        if (newsEntity.getArticleThumb() != null && newsEntity.getArticleThumb().length > 0) {
            jcVideoPlayer.setUp(newsEntity.getVideoCurl(), JCVideoPlayer.SCREEN_LAYOUT_LIST,
                    newsEntity.getArticleTitle(), newsEntity.getArticleThumb()[0]);
        } else {
            jcVideoPlayer.setUp(newsEntity.getVideoCurl(), JCVideoPlayer.SCREEN_LAYOUT_LIST,
                    newsEntity.getArticleTitle(), "");
        }
        jcVideoPlayer.setShareBean(newsEntity, newsEntity.getUrlStatus());
        if (!jcVideoPlayer.isPlaying() && !TextUtils.isEmpty(newsEntity.getPeriod())) {
            this.period.setText(newsEntity.getPeriod());
            this.period.setVisibility(View.VISIBLE);
        } else {
            this.period.setVisibility(View.GONE);
        }
        if (attrImg != null && (TextUtils.isEmpty(newsEntity.getAttrIcon()) || jcVideoPlayer.isPlaying())) {
            if (this.attrImg.getVisibility() == View.VISIBLE) {
                this.attrImg.setVisibility(View.GONE);
            }
        } else {
            //加载图片
            HttpUtils.addImageRequest(newsEntity.getAttrIcon(), "", 0, 0, new Response.Listener<Bitmap>() {

                @Override
                public void onResponse(Bitmap response) {
                    if (response != null && attrImg != null) {
                        attrImg.setVisibility(View.VISIBLE);
                        attrImg.setImageBitmap(response);

                    }
                }
            }, null);
        }
    }

    @Override
    public void onEvent(int type, String url, int screen, Object... objects) {
        if (type == ON_CLICK_START_ICON || type == ON_CLICK_START_THUMB) {
            if (this.period != null && this.period.getVisibility() == View.VISIBLE) {
                this.period.setVisibility(View.GONE);
            }
            if (this.attrImg != null && this.attrImg.getVisibility() == View.VISIBLE) {
                this.attrImg.setVisibility(View.GONE);
            }
        }
    }

}
